package com.shangjun;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by sahngjun on 2017/3/21.
 */
public class TestProductorAndConsumer {
}
class Clerk{
    public int modCount=0;
    public Lock lock = new ReentrantLock();
    public Condition condition=lock.newCondition();
    //进货
    public void get(){
        lock.lock();
        try {
            while(modCount>=1){
                System.out.println("商品已满");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+":"+ ++modCount);
            condition.signalAll();
        } finally {
            lock.unlock();
        }

    }
    //卖货
    public void sall(){
        lock.lock();
        try {
            while(modCount<=1){
                System.out.println("无货");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+":"+ ++modCount);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

}

class Productor{

}

class Consumer{

}
